"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function xhr(type, url, data) {
  return new Promise(function (fullfill, reject) {
    // 判断这个请求是否需要携带数据;
    if (_typeof(data) === "object" && data !== null && !(data instanceof Array)) {
      var temp = "";

      for (var attr in data) {
        temp += "&" + attr + "=" + data[attr];
      }

      data = temp.slice(1);
    } // 请求方式为get时，同时传递了data数据


    if (type === "GET" && typeof data === "string") {
      url += "?" + data;
    }

    var xhr = new XMLHttpRequest();
    xhr.open(type, url); // 判定请求方式为post，且携带了data数据; 

    if (type === "POST" && typeof data === "string") {
      xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
      xhr.send(data);
    } else {
      xhr.send();
    }

    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        // 如果请求成功则调用回调函数; 
        fullfill(xhr.responseText);
      }
    };
  });
}